package com.sk.weichat.ui.me.wallet;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.sk.weichat.R;
import com.sk.weichat.helper.DialogHelper;

import com.sk.weichat.ui.base.EditBaseActivity;
import com.sk.weichat.util.ToastUtil;
import com.sk.weichat.view.PasswordInputEditText;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;
import java.util.HashMap;
import java.util.Map;
import de.greenrobot.event.EventBus;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.Call;

/**
* 重新输入密码
* */
public class SetPayPwdInputAgainActivity extends EditBaseActivity implements View.OnClickListener {

    private ImageView ivBack;
    private TextView tv_title_center;
    private PasswordInputEditText edtPswd;
    private Button btnConfirm;
    private String newPswd;
    private String PayPasswordType;

    private boolean once;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_pay_pwd_input_again);
        getSupportActionBar().hide();
        newPswd = getIntent() != null ? getIntent().getStringExtra("pswd") : "";
        PayPasswordType = getIntent().getStringExtra("PayPasswordType");
        initView();
    }
    private  void initView(){
        ivBack = findViewById(R.id.iv_title_left);
        tv_title_center = findViewById(R.id.tv_title_center);
        tv_title_center.setText(getString(R.string.set_pay_pwd));
        ivBack.setOnClickListener(this);
        edtPswd = findViewById(R.id.edt_pswd);
        edtPswd.setTextWatcher(new Observer<CharSequence>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(CharSequence charSequence) {
                String pswd = charSequence.toString();
                if (pswd.length() == 6) {
                    if (pswd.equals(newPswd)) {
                        //两次密码相同，
                        btnConfirm.setEnabled(true);
                        btnConfirm.setBackgroundResource(R.drawable.shape_corner_26dp_solid_primary);
                    } else {
                        if(once){
                            ToastUtil.showToast(mContext, getString(R.string.confirm_two_failed));
                            setResult(Activity.RESULT_CANCELED);
                            finish();
                            return;
                        }
                        once = true;
                        //两次密码不相同
                        btnConfirm.setEnabled(false);
                        edtPswd.setText("");
                        ToastUtil.showToast(mContext, getString(R.string.input_pswd_confirm_incorrect));
                        btnConfirm.setBackgroundResource(R.drawable.shape_corner_26_solid_gray_bdc2c8);
                    }
                } else {
                    btnConfirm.setEnabled(false);
                    btnConfirm.setBackgroundResource(R.drawable.shape_corner_26_solid_gray_bdc2c8);
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
        btnConfirm = findViewById(R.id.btn_confirm);
        btnConfirm.setEnabled(false);
        btnConfirm.setOnClickListener(this);
        //弹出键盘输入
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

    @Override
    public void onClick(View v) {

        if (v==ivBack){
            finish();
        }else if (v==btnConfirm){
            setPassWord();
        }
    }

    /**设置支付密码
    *
    * */
    private void setPassWord(){
        if (!DialogHelper.isShowing()) {
            DialogHelper.showDefaulteMessageProgressDialog(this);
        }
        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("payPassword", edtPswd.getText());
        params.put("affirmPassword", edtPswd.getText());
        params.put("setPayPasswordType",PayPasswordType);

        HttpUtils.post().url(coreManager.getConfig().SET_PAY_PASSWORD)
                .params(params)
                .build()
                .execute(new BaseCallback<String>(String.class) {

                    @Override
                    public void onResponse(ObjectResult<String> result) {
                        DialogHelper.dismissProgressDialog();

                        if (result.getResultCode()==1){
                            EventBus.getDefault().post("set_pay_psd_success");
                            ToastUtil.showToast(SetPayPwdInputAgainActivity.this, R.string.set_pay_psd_success);
                            setResult(Activity.RESULT_OK);
                            finish();
                        }else {
                            ToastUtil.showToast(SetPayPwdInputAgainActivity.this,result.getResultMsg());
                        }
                    }
                    @Override
                    public void onError(Call call, Exception e) {
                        Log.e("AlipayAutoMsg", "Exception", e);
                        ToastUtil.showToast(SetPayPwdInputAgainActivity.this,e.toString());
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }
}
